package com.mxdx.edu.controller;

import com.alibaba.fastjson.JSONObject;
import com.mxdx.edu.common.ApiResult;
import com.mxdx.edu.common.CommonUtils;
import com.mxdx.edu.common.log.AppLogger;
import com.mxdx.edu.enums.ErrorEnums;
import com.mxdx.edu.service.ISemesterService;
import com.mxdx.edu.service.IStudentService;
import com.mxdx.edu.util.RedisUtil;
import com.mxdx.edu.util.ServletUtil;
import com.mxdx.edu.util.SessionUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: liujianlu
 * @Description: 查看成绩
 * @Date: Created in 15:16 2018/6/28
 */
@Controller
@RequestMapping(value = "/studentScore")
public class ViewScoreController extends BaseController{

    private AppLogger logger = new AppLogger(ViewScoreController.class);

    @Resource
    protected SessionUtil sessionUtil;

    @Resource
    protected RedisUtil redisUtil;

    @Resource
    protected IStudentService studentService;

    @Resource
    protected ISemesterService semesterService;

    /**
     *   考试首页
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/auth/studentScoreIndex")
    public ModelAndView viewScoreIndex(HttpServletRequest request){
        logger.entry(ServletUtil.getClientIp(request), "studentScoreIndex");
        ApiResult<Map<String, Object>> apiResult = new ApiResult<>();
        ModelAndView result = new ModelAndView("/h5/studentScoreIndex");
        List<Map<String, Object>> semesteList = new ArrayList<>();
        String studentName = "";
        String headPortrait = "";
        String mood = "";
        //String semesterName = "";
        //String semesterId = "";
        try{
            Map<String,Object> userMap = getStudentMap(request);
            String studentId = CommonUtils.getStrValueFromMap(userMap,"studentId","");
            headPortrait = CommonUtils.getStrValueFromMap(userMap, "headPortrait", "");
            studentName = CommonUtils.getStrValueFromMap(userMap, "studentName", "");
            mood = CommonUtils.getStrValueFromMap(userMap, "mood", "");

            //通过学生id查询学生信息
            Map<String, Object> findStudentMap = new HashMap<>();
            findStudentMap.put("studentId", studentId);
            Map<String, Object> studentMap = studentService.findStudentMapOneByMap(findStudentMap);
            String classId = CommonUtils.getStrValueFromMap(studentMap, "classId", "");
            //课程信息
            semesteList = semesterService.findScoreListByMap(classId,studentId);
            //semesterName = CommonUtils.getStrValueFromMap(semesteList.get(0), "semesterName", "");
            //semesterId = CommonUtils.getStrValueFromMap(semesteList.get(0), "id", "");
            logger.info(ServletUtil.getClientIp(request));
        }catch (Exception e){
            super.dealException(ErrorEnums.SYSTEM_ERROR, e, apiResult, logger);
        }
        result.addObject("courseList", semesteList);
        result.addObject("studentName", studentName);
        result.addObject("headPortrait", headPortrait);
        result.addObject("mood", mood);
        //result.addObject("semesterName", semesterName);
        //result.addObject("semesterId", semesterId);

        return result;
    }



}
